package net.androy.hoola;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class Daily extends ListActivity {

    private boolean showingHoroscopes = false;
    private Map<String, String> dailyTitles = new HashMap<String, String>();
    List<HoroscopeItem> horoscopeItems = new ArrayList<HoroscopeItem>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	buildDailyTitles();

	List<String> list = new ArrayList<String>();
	list.addAll(dailyTitles.values());
	Collections.sort(list);

	setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list.toArray(new String[0])));
	getListView().setTextFilterEnabled(true);
    }
    
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Log.i("PRESSED::: ", keyCode+"");
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	// TODO Auto-generated method stub
	return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onListItemClick(ListView l, View v, final int position, long id) {

	if (!showingHoroscopes) {
	    setProgressBarIndeterminateVisibility(true);
	    final Handler handler = new Handler();
	    Runnable r = new Runnable() {
		public void run() {
		    // retrieve the feed
		    FeedRetriever.getFeed(horoscopeItems, dailyTitles.keySet().toArray(new String[0])[position]);

		    handler.post(new Runnable() {
			public void run() {
			    Daily.this.setListAdapter(new CustomAdapter(Daily.this, horoscopeItems));
			    Daily.this.getListView().setTextFilterEnabled(true);
			    Daily.this.setProgressBarIndeterminateVisibility(false);
			}
		    });
		}
	    };

	    new Thread(r).start();
	    showingHoroscopes = true;

	} else {
	    ((CustomAdapter) getListAdapter()).toggle(position);
	}
    }

    private void buildDailyTitles() {
	dailyTitles.put("overview", "Overview");
	dailyTitles.put("dailycoupleslove", "Couple's Love");
	dailyTitles.put("dailycosmic", "Cosmic Calendar");
	dailyTitles.put("dailychinese", "Chinese");
	dailyTitles.put("dailybabyscope", "BabyScope");
	dailyTitles.put("dailyhomeandgarden", "Home & Garden");
	dailyTitles.put("dailygreenscope", "GreenScope");
	dailyTitles.put("dailymomscope", "MomScope");
	dailyTitles.put("dailyextended", "Extended");
	dailyTitles.put("dailyflirt", "Flirt");
	dailyTitles.put("dailyquickie", "Quickie");
	dailyTitles.put("dailybeautyscope", "BeautyScope");
	dailyTitles.put("dailycatscope", "CatScope");
	dailyTitles.put("dailygayscope", "GayScope");
	dailyTitles.put("dailyfoodscope", "FoodScope");
	dailyTitles.put("dailysingleslove", "Chinese");
	dailyTitles.put("dailysingleslove", "Single's Love");
	dailyTitles.put("dailywork", "Work");
	dailyTitles.put("dailyteenhoroscopes", "Teen");
	dailyTitles.put("dailyastroslam", "AstroSlam");
	dailyTitles.put("dailydogscope", "DogScope");
	dailyTitles.put("dailylesbianscope", "LesbianScope");
	dailyTitles.put("dailyfinance", "FinanceScope");
    }

    private class CustomAdapter extends BaseAdapter {
	private List<HoroscopeItem> items;
	private Context ctx;
	private Boolean[] mExpanded = null;

	public CustomAdapter(Context context, List<HoroscopeItem> items) {
	    this.items = items;
	    List<Boolean> e = new ArrayList<Boolean>();
	    for (int i = 0; i < items.size(); i++) {
		e.add(false);
	    }

	    mExpanded = e.toArray(new Boolean[0]);

	    ctx = context;
	}

	public void toggle(int position) {
	    mExpanded[position] = !mExpanded[position];
	    notifyDataSetChanged();
	}

	public int getCount() {
	    return items.size();
	}

	public Object getItem(int position) {
	    return items.get(position);
	}

	public long getItemId(int position) {
	    // TODO Auto-generated method stub
	    return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
	    HoroscopeView sv;
	    if (convertView == null) {
		sv = new HoroscopeView(ctx, items.get(position).getTitle(), items.get(position).getDescription(),
			mExpanded[position]);
	    } else {
		sv = (HoroscopeView) convertView;
		sv.setTitle(items.get(position).getTitle());
		sv.setDialogue(items.get(position).getDescription());
		sv.setExpanded(mExpanded[position]);
	    }

	    return sv;
	}

    }

    private class HoroscopeView extends LinearLayout {
	public HoroscopeView(Context context, String title, String dialogue, boolean expanded) {
	    super(context);

	    this.setOrientation(VERTICAL);

	    mTitle = new TextView(context);
	    mTitle.setText(title);
	    mTitle.setTextSize(14);
	    mTitle.setTextColor(Color.WHITE);

	    addView(mTitle, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 40));
	    mDialogue = new TextView(context);
	    mDialogue.setText(dialogue);
	    addView(mDialogue, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

	    mDialogue.setVisibility(expanded ? VISIBLE : GONE);
	}

	/**
	 * Convenience method to set the title of a SpeechView
	 */
	public void setTitle(String title) {
	    mTitle.setText(title);
	}

	/**
	 * Convenience method to set the dialogue of a SpeechView
	 */
	public void setDialogue(String words) {
	    mDialogue.setText(words);
	}

	/**
	 * Convenience method to expand or hide the dialogue
	 */
	public void setExpanded(boolean expanded) {
	    mDialogue.setVisibility(expanded ? VISIBLE : GONE);
	}

	private TextView mTitle;
	private TextView mDialogue;
    }

}
